package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MoveFile {
	
    public static String moveFile(String shopId, String imgViewPath) throws IOException {
    	String fileName="";
    	imgViewPath = imgViewPath.replaceAll("\\\\", "/");
    	if(imgViewPath.contains("/")){   
    		fileName = shopId+"/"+imgViewPath.substring(imgViewPath.lastIndexOf("/")+1);   
    	}
		InputStream input=null;
		FileOutputStream os=null;
		try {
			input = new FileInputStream(imgViewPath);
			String base =  GetConfig.getLocalFilePath();
			File file = new File(base+shopId);//创建文件夹
			if(!file.exists()){
				file.mkdirs();
			}
			os = new FileOutputStream(base+fileName);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
        try {    
             byte temp[] = new byte[1024];    
             int n = 0;    
             while ((n = input.read(temp)) != -1) {    
                 os.write(temp, 0, n);    
             }    
         } catch (Exception e) {    
             System.out.print("文件"+imgViewPath+"转移出错了");    
             throw e;
         } finally {    
            if (os != null)    
                 os.close();    
            if (input != null)    
            	input.close();    
         }    		
		return fileName;
		
	}
}
